/**
 * Copyright (c) 2020 qar All rights reserved.
 * <p>
 * https://www.qar.io
 * <p>
 * 版权所有，侵权必究！
 */

package com.example.controller;

import com.example.dto.GenBaseClassDTO;
import com.example.entity.BaseClassEntity;
import com.example.model.api.Result;
import com.example.model.page.PageData;
import com.example.service.BaseClassService;
import com.example.utils.ConvertUtils;
import com.example.validator.AssertUtils;
import com.example.validator.ValidatorUtils;
import com.example.validator.group.AddGroup;
import com.example.validator.group.DefaultGroup;
import com.example.validator.group.UpdateGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 基类管理
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("baseclass")
@Tag(name = "基类管理")
public class BaseClassController {
    @Autowired
    private BaseClassService baseClassService;

    @GetMapping("page")
    @Operation(summary = "分页")
    public Result<PageData<GenBaseClassDTO>> page(@RequestParam Map<String, Object> params) {
        PageData<GenBaseClassDTO> page = baseClassService.page(params);

        return new Result<PageData<GenBaseClassDTO>>().ok(page);
    }

    @GetMapping("list")
    @Operation(summary = "列表")
    public Result<List<GenBaseClassDTO>> list() {
        List<GenBaseClassDTO> list = baseClassService.list();

        return new Result<List<GenBaseClassDTO>>().ok(list);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    public Result<GenBaseClassDTO> get(@PathVariable("id") Long id) {
        AssertUtils.isNull(id, "不能为空");
        BaseClassEntity baseClassEntity = baseClassService.selectById(id);
        return new Result<GenBaseClassDTO>().ok(ConvertUtils.sourceToTarget(baseClassEntity, GenBaseClassDTO.class));
    }

    @PostMapping
    @Operation(summary = "保存")
    public Result<String> save(@RequestBody GenBaseClassDTO genBaseClassDTO) {
        ValidatorUtils.validateEntity(genBaseClassDTO, AddGroup.class, DefaultGroup.class);
        baseClassService.insert(ConvertUtils.sourceToTarget(genBaseClassDTO, BaseClassEntity.class));

        return new Result<String>().ok("ok");
    }

    @PutMapping
    @Operation(summary = "修改")
    public Result<String> update(@RequestBody GenBaseClassDTO genBaseClassDTO) {
        ValidatorUtils.validateEntity(genBaseClassDTO, UpdateGroup.class, DefaultGroup.class);
        baseClassService.updateById(ConvertUtils.sourceToTarget(genBaseClassDTO, BaseClassEntity.class));

        return new Result<String>().ok("ok");
    }

    @DeleteMapping
    @Operation(summary = "删除")
    public Result<String> delete(@RequestBody Long[] ids) {
        AssertUtils.isListEmpty(Arrays.asList(ids), "不能为空");
        baseClassService.deleteBatchIds(Arrays.asList(ids));

        return new Result<String>().ok("ok");
    }
}